package com.org.manhu.chargehelper.ui.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.org.manhu.chargehelper.R;
import com.org.manhu.chargehelper.adapter.AppListAdapter;
import com.org.manhu.chargehelper.broadcastreceiver.AcceptDataBroadcast;
import com.org.manhu.chargehelper.constant.Constant;
import com.org.manhu.chargehelper.custom.XListView;

/**
 * @文件名：ExchangeListFragment.java
 * @包名：com.org.manhu.center
 * @作者：李朋辉 @QQ：892839211
 * @创建时间：2015年1月27日 上午11:55:25
 *
 * @描述：用来加载盒子推送过来的应用列表的fragment
 */
public class AppListFragment extends Fragment {

	private View view;
	private XListView mListView;
	private LinearLayout loadLayout;
	private AppListAdapter adapter;
	
	List<Map<String, String>> list = new ArrayList<Map<String, String>>();

	private AcceptDataBroadcast receiver;
	private String type;
	
	public AppListFragment(String type){
		this.type = type;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		view = inflater.inflate(R.layout.fragment_exchange_list, null);
		mListView = (XListView) view.findViewById(R.id.exchage_good_list);
		loadLayout = (LinearLayout) view
				.findViewById(R.id.exchage_good_load_layout);

		loadLayout.setVisibility(View.GONE);
		mListView.setVisibility(View.VISIBLE);
		mListView.setPullLoadEnable(false);
		mListView.setPullRefreshEnable(false);
		adapter = new AppListAdapter(getActivity(), list,type);
		mListView.setAdapter(adapter);

		IntentFilter filter = new IntentFilter(Constant.ACTION_ACCEPT_DATA);
		receiver = new AcceptDataBroadcast(adapter);
		getActivity().registerReceiver(receiver, filter);

		return view;
	}

	public void setData(List<Map<String, String>> data){
		this.list.addAll(data);
	}
	
	public void notify(List<Map<String, String>> data){
		this.adapter.setData(data);
		this.adapter.notifyDataSetChanged();
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		 getActivity().unregisterReceiver(receiver);
	}

}
